package service;

import java.util.List;
import java.util.Map;

import base.Service;
import bean.Warehouse;

public class WarehouseS extends Service
{
	public int getCount(String columnName,String keyword)
	{
		 String sql="select count(*) from warehouse ";
	     if(!"".equals(columnName)&&!"".equals(keyword))
		      sql+=" where "+columnName+" like '%"+keyword+"%'";
	     return jdbc.queryForInt(sql);
	}
	public List getPage(int amount,int pageSize,int pageNum,String columnName,String keyword)
	{
		  String sql="select * " +
   		             "from (select top "+(pageSize*pageNum>amount?(amount-pageSize*(pageNum-1)):pageSize)+" * "+
   		                   "from(select top "+pageSize*pageNum+" wname,wnumber,wremark "+
   		                        "from warehouse ";
                      if(!"".equals(columnName)&&!"".equals(keyword))
		                  sql+=" where "+columnName+" like '%"+keyword+"%' ";
                           sql+="order by wnumber asc)temp1 "+
                           "order by wnumber desc)tmep2 "+
                      "order by wnumber desc ";
          return jdbc.queryForList(sql);
	}
	public List getAll()
	{
		String sql="select * from warehouse";
		return jdbc.queryForList(sql);
	}
	public Map getWare(String number)
	{
		  String sql="select * from warehouse where wnumber='"+number+"'";
		  return jdbc.queryForMap(sql);	
	}
	public void save(Warehouse warehouse)
	{
		  String sql="";
  	      if(0==warehouse.getId())
		  {
			   
			   sql="insert into warehouse(wnumber,wname,wremark) values("+
	                              "'"+warehouse.getNumber()+"',"+
	                              "'"+warehouse.getName()+"',"+
	                              "'"+warehouse.getRemark()+"')";
	      }
		  else
		  {
			   sql="update warehouse set wnumber='"+warehouse.getNumber()+"',"+
			   		                      "wname='"+warehouse.getName()+"',"+
	                                    "wremark='"+warehouse.getRemark()+"'"+
	               " where wid="+warehouse.getId() ;
		  }
		  //System.out.println(sql);
		  jdbc.execute(sql);
	}
	public boolean delWare(String number)
	{     String testSql="select sum(samount) from warehouse,stock where wnumber=swarenumber and wnumber='"+number+"'";
	      //System.out.println(testSql);
	      int amount=jdbc.queryForInt(testSql);
	      if(amount>0)
		  {
            return false;			
		  }   
		  else
		  {
			 String sql="delete from warehouse where wnumber='"+number+"'";
	         jdbc.execute(sql);
	         return true;
		  }
	}
}
